//
//  YH_AdverCatViewController.m
//  YinHang
//
//  Created by 张亚哲 on 14/10/25.
//  Copyright (c) 2014年 孙彦鹏. All rights reserved.
//

#import "YH_AdverCatViewController.h"
//切换图片
#import "ChangePicViewController.h"
//其他广告位
#import "OtherCatViewController.h"

#import "PicCatViewController.h"


@interface YH_AdverCatViewController ()<adPushDelegate>{
    NSInteger selectedIndex;
    
    __weak IBOutlet UIView *ChangeView;
    OtherCatViewController *otherCatView;//其他广告位视图
    ChangePicViewController *changePic;//切换图片视图
    
    UIButton *addBtn;//添加按钮
}

@end

@implementation YH_AdverCatViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    switch (selectedIndex) {
        case 0:{
            if (changePic) {
                [changePic refreshTableView];
            }
        }break;
        case 1:{
            if (otherCatView) {
                [otherCatView refreshTableView];
            }
        }break;
        default:
            break;
    }
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self setNavTitle];
    
    [self setSegment];
    
    [self setChangeView];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark CustomMethod

-(void)setNavTitle{
    //标题
    self.navigationItem.title = @"广告位管理";
    
    UIButton *leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 20, 30)];
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSpacer.width = - 10;
    if (ios7) {
        self.navigationItem.leftBarButtonItems = @[negativeSpacer,leftBarButtonItem];
    }else{
        self.navigationItem.leftBarButtonItems = @[leftBarButtonItem];
    }
    
    //添加
    addBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    addBtn.frame = CGRectMake(0, 0, 60, 35);
    addBtn.hidden = YES;
    UIImage *postBtn_bgImg = [UIImage imageNamed:@"postbtn_bg"];
    [addBtn setTitle:@"添加" forState:UIControlStateNormal];
    [addBtn setTintColor:[UIColor whiteColor]];
    [addBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [addBtn setBackgroundImage:[postBtn_bgImg stretchableImageWithLeftCapWidth:postBtn_bgImg.size.width*0.5f topCapHeight:postBtn_bgImg.size.height*0.5f] forState:UIControlStateNormal];
    [addBtn addTarget:self action:@selector(add) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *addBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:addBtn];
    if (ios7) {
        self.navigationItem.rightBarButtonItems = @[negativeSpacer,addBarButtonItem];
    }else{
        self.navigationItem.rightBarButtonItems = @[addBarButtonItem];
    }
    
}

-(void)setChangeView{
    otherCatView = [[OtherCatViewController alloc]initWithNibName:@"OtherCatViewController" bundle:nil];
    otherCatView.delegate = self;
    [ChangeView addSubview:otherCatView.view];
    
    changePic = [[ChangePicViewController alloc]initWithNibName:@"ChangePicViewController" bundle:nil];
    changePic.delegate = self;
    [ChangeView addSubview:changePic.view];
    
}

-(void)setSegment{
    // 加载选项卡
    HMSegmentedControl *segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"切换图片", @"其他广告位"]];
    selectedIndex = 0;
    [segmentedControl setIndexChangeBlock:^(NSUInteger index) {
        
        selectedIndex = index;
        NSLog(@"%i",index);
        switch (selectedIndex) {
            case 0:{
                [ChangeView bringSubviewToFront:changePic.view];
                [changePic refreshTableView];
                addBtn.hidden = YES;//切换图片时隐藏
            }break;
            case 1:{
                addBtn.hidden = NO;//广告位时加载
                [otherCatView refreshTableView];
                [ChangeView bringSubviewToFront:otherCatView.view];
            }break;
            default:
                break;
        }
        
    }];
    [segmentedControl setFrame:CGRectMake(0, 0, self.view.bounds.size.width, 35)];
    [self.view addSubview:segmentedControl];
}

-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark AdPushDelegate Method

-(void)push:(UIViewController *)viewController{
    [self.navigationController pushViewController:viewController animated:YES];
}
//进入加载页面
-(void)add{
    PicCatViewController *PCVC = [[PicCatViewController alloc]initWithNibName:@"PicCatViewController" bundle:nil];
    //设置属性
    PCVC.state = @"add";
    [self.navigationController pushViewController:PCVC animated:YES];
}

-(void)dealloc{
    otherCatView = nil;
    changePic = nil;
}
@end
